'use strict';

exports.index = async ctx => {
  let page = 1;
  let pageSize = 2;
  let result = await ctx.app.elasticsearch.search({
    index: 'news',
    from: (page - 1) * pageSize, //skip
    size: pageSize,
    body: {
      query: {
        match: {
          content: 'add'
        }
      }
    }
  });
  ctx.sendSuccess(result);
};

exports.create = async (ctx) => {
  let addResult = await ctx.app.elasticsearch.bulk({
    body: [
      { index: { _index: 'news', _id: '111111111111111115' } },
      { content: 'es-add-test5' }
    ]
  });
  ctx.sendSuccess(addResult);
};

exports.update = async (ctx) => {
  let editResult = await ctx.app.elasticsearch.bulk({
    body: [
      { update: { _index: 'news', _id: '111111111111111111' } },
      { doc: { content: '使用ARM芯片的Mac将可能在2020年推出666' } },
    ]
  });
  ctx.sendSuccess(editResult);
};

exports.destroy = async (ctx) => {
  let result = await ctx.app.elasticsearch.bulk({
    body: [
      { delete: { _index: 'news', _id: '111111111111111111' } },
    ]
  });
  ctx.sendSuccess(result);
}
